package com.ky.serialportpackage;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.ky.serialport.sensor.DianYaBianSongQiAckBean;
import com.ky.serialport.sensor.DianYaBianSongQiBean;
import com.ky.serialport.sensor.Dt805DataBean;
import com.ky.serialport.sensor.DtConnectBean;
import com.ky.serialport.sensor.Dt405_410DataBean;
import com.ky.serialport.sensor.DtDisConnectBean;
import com.ky.serialport.sensor.UldConnectedBean;
import com.ky.serialport.sensor.UldDataBean;
import com.ky.serialport.sensor.UldDisconnectBean;
import com.ky.serialport.sensor.WenDuBean;
import com.ky.serialport.service.BlueToothDtUldService;
import com.ky.serialport.service.SerialPortService;
import com.ky.serialport.socket.Socket2Activity;
import com.ky.serialport.socket.SocketActivity;
import com.ky.serialport.utils.MyFunc;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.ky.serialport.utils.FormatUtils.df22;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.btn_port)
    Button btnPort;
    @BindView(R.id.btn_socket_1)
    Button btnSocket1;
    @BindView(R.id.btn_socket_2)
    Button btnSocket2;
    private Intent intent;
    private Intent dtUldIntent;

    private String TAG = this.getClass().getName()+"123";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        EventBus.getDefault().register(MainActivity.this);

        intent = new Intent();
        intent.setClass(MainActivity.this,SerialPortService.class);
        startService(intent);

        dtUldIntent = new Intent();
        dtUldIntent.setClass(MainActivity.this,BlueToothDtUldService.class);
        startService(dtUldIntent);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void OnConntedDtData(DtConnectBean dtConnectBean) {
        Log.d(TAG, "OnGetBtData: ------>  连接到德图传感器");
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void OnDisconntedDtData(DtDisConnectBean dtDisConnectBean) {
        Log.d(TAG, "OnGetBtData: ------>  断开德图传感器 ");
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void OnGetDtData(Dt405_410DataBean dtDataBean) {
        Log.d(TAG, "OnGetBtData: " + df22.format(dtDataBean.wenduVel) + " ℃    \n" + df22.format(dtDataBean.fengsuVel) + " m/s");
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void OnGet805Data(Dt805DataBean dt805DataBean) {
        Log.d(TAG, "OnGetBtData:温度数据 " + dt805DataBean.name + MyFunc.ByteArrToHex(dt805DataBean.data));
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void OnConntedUldData(UldConnectedBean uldConnectedBean) {
        Log.d(TAG, "OnGetBtData: ------>  连接光照传感器 ");
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void OnDisconntedUldData(UldDisconnectBean uldDisconnectBean) {
        Log.d(TAG, "OnGetBtData: ------>  断开光照传感器 ");
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void OnGetUldData(UldDataBean uldDataBean) {
        Log.d(TAG, "OnGetUldData: ------》 获取光照数据" + MyFunc.ByteArrToHex(uldDataBean.data));
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onDianYaBianSongQi(DianYaBianSongQiBean event) {
        Log.d("串口数据123", "onMessageEvent: --------------------->>>>>> GYD50W矿用本安型无线电压变送器");
       // sv50v.setData(new SensorData().setPower(event.power).setSignal(event.signal).setStatus((sv50v.isTesting() || sv50v.isCompleted()) ? sv50v.getSensorStatus() : SensorInf.NORMAL));
    }



    @OnClick({R.id.btn_port, R.id.btn_socket_1, R.id.btn_socket_2})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_port:
                break;
            case R.id.btn_socket_1: //采用Bind方式启动
                startActivity(new Intent(MainActivity.this,SocketActivity.class));
                break;
            case R.id.btn_socket_2: //采用start方式启动
                startActivity(new Intent(MainActivity.this,Socket2Activity.class));
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopService(intent);
        stopService(dtUldIntent);
        EventBus.getDefault().unregister(MainActivity.this);
    }
}


